package _Pan3D.program
{
	import _Pan3D.program.shaders.BaseShader;
	import _Pan3D.program.shaders.BuildShader;
	import _Pan3D.program.shaders.BuildTreeShader;
	import _Pan3D.program.shaders.DiffuseShader;
	import _Pan3D.program.shaders.DynmicUVShader;
	import _Pan3D.program.shaders.FensanBiandaShader;
	import _Pan3D.program.shaders.FensanShader;
	import _Pan3D.program.shaders.FireBallShader;
	import _Pan3D.program.shaders.GlassShader;
	import _Pan3D.program.shaders.GroundScanningHightShader;
	import _Pan3D.program.shaders.GroundShader;
	import _Pan3D.program.shaders.KuosanAndBiandaShader;
	import _Pan3D.program.shaders.Md5Shader;
	import _Pan3D.program.shaders.NewScanningShader;
	import _Pan3D.program.shaders.NormalShader;
	import _Pan3D.program.shaders.ScanningShader;
	import _Pan3D.program.shaders.ScanningShadow;
	import _Pan3D.program.shaders.ShadowShader;
	import _Pan3D.program.shaders.StatShader;
	import _Pan3D.program.shaders.TestFensanBiandaShader;
	import _Pan3D.program.shaders.UvMoveNewShader;
	import _Pan3D.program.shaders.WillDeleShader;
	import _Pan3D.program.shaders.ZhuanQuanShader;
	
	import _me.Scene_data;
	
	import com.adobe.utils.AGALMiniAssembler;
	
	import flash.display3D.Context3D;
	import flash.display3D.Program3D;
	
	public class Program3DManager
	{
		private static var instance:Program3DManager;
		private var _context3D:Context3D;
		private var _programDic:Object;
		private var _classDic:Object;
		private var _shaderAssembler:AGALMiniAssembler;
		public function Program3DManager()
		{
			this._context3D = Scene_data.context3D;
			_programDic = new Object;
			_classDic = new Object;
			_shaderAssembler = new AGALMiniAssembler();
		}
		public static function getInstance():Program3DManager{
			if(!instance){
				instance = new Program3DManager();
			}
			return instance;
		}
		
		public function initReg():void{
			//注册着色器
			registe(Md5Shader.MD5SHADER,Md5Shader);
			registe(StatShader.STATSHADER,StatShader);
			registe(ScanningShader.SCANNINGSHADER,ScanningShader);
			registe(NormalShader.NORMALSHADER,NormalShader);
			registe(ScanningShadow.SCANNINGSHADOW,ScanningShadow);
			registe(ShadowShader.SHADOWSHADER,ShadowShader);
			registe(BaseShader.BASESHADER,BaseShader);
			registe( BuildShader.BUILD_SHADER,BuildShader);
			registe(  NewScanningShader.NEW_SCANNING_SHADER,NewScanningShader);
			registe( GroundShader.GROUND_SHADER,GroundShader)
			registe( FensanShader.FENSAN_SHADER,FensanShader)
			registe(DynmicUVShader.DYNMICUVSHADER,DynmicUVShader)
			registe(FensanBiandaShader.FENSAN_BIANDA_SHADER,FensanBiandaShader)
			registe(DiffuseShader.DIFFUSESHADER,DiffuseShader);
			registe(UvMoveNewShader.UVMOVE_NEW_SHADER,  UvMoveNewShader);
			registe(ZhuanQuanShader.ZHUANQUAN_SHADER,  ZhuanQuanShader);
			registe(TestFensanBiandaShader.TEST_FENSAN_BIANDA_SHADER,  TestFensanBiandaShader);
			registe(KuosanAndBiandaShader.KUOSAN_AND_BIANDA_SHADER,  KuosanAndBiandaShader);
			registe(GroundScanningHightShader.GROUND_SCANNING_HIGHT_SHADER,  GroundScanningHightShader);
			registe(GlassShader.GLASS_SHADER,   GlassShader);
			registe( WillDeleShader.WILL_DELE_SHADER,   WillDeleShader);
			registe( BuildTreeShader.BUILD_TREE_SHADER,BuildTreeShader)


		}
		public function getProgram(key:String):Program3D{
			if(_programDic[key]){
				return _programDic[key];
			}else{
				if(_classDic[key]){
					var cls:Class = _classDic[key];
					var shader:IShader3D = new cls();
					shader.encode(_shaderAssembler);
					var program:Program3D = _context3D.createProgram();
					program.upload(shader.vertexShaderByte,shader.fragmentShaderByte);
					_programDic[key] = program;
					return program;
				}else{
					throw new Error("未注册的着色器:" + key)
				}
			}
			return null;
			
			
		}
		
		private function registe(key:String,shaderCls:Class):void{
			_classDic[key] = shaderCls;
		}
		
	}
}